package Client.Manager
{
	import Client.VO.ArmyVO;
	import Client.VO.HouseVO;
	import Client.Utils.Utils;
	
	/**
	 * class		HouseMgr
	 * @desc		used to manage the houses
	 * @author		Aqua
	 * @date		2011-9-7
	 */
	public class HouseMgr
	{
		//---------------------//
		//      Singleton      //
		//---------------------//
		
		/**************************************************************************
		 * function				instance getter
		 * @desc				a private function used to enforce singlton
		 * @return	HouseMgr	singleton instance of this class
		 *************************************************************************/
		public static function get instance():HouseMgr
		{
			if(null == _instance)
			{
				_instance = new HouseMgr(singleton);
			}
		
			return _instance;
		}
		
		/**************************************************************************
		 * function			singleton
		 * @desc			a private function used to enforce singlton
		 *************************************************************************/
		private static function singleton():void
		{
			
		}
		
		
		//---------------------//
		//     Constructor     //
		//---------------------//
		
		/**************************************************************************
		 * function			HouseMgr
		 * @desc			constructor of class
		 * @param	s		a private function used to enforce singlton
		 *************************************************************************/
		public function HouseMgr(s:Function = null):void
		{
			if (singleton != s)
			{
				throw new Error("This is a singleton class!");
			}
		}
		
		
		//-----------------//
		//     Publics     //
		//-----------------//
		
		/**************************************************************************
		 * function				loadHouse
		 * @desc				load infomation of all houses in a specified map
		 * @param	xmlList		XML file that specifies areas' IDs
		 *************************************************************************/
		public function loadHouse(xmlList:XMLList):void
		{
			var item		:XML;
			var dataXML		:XML;
			var dataList	:XMLList;
			var cardList	:XMLList;
			var areaList	:XMLList;
			var vo			:HouseVO;
			var useNewCard	:Boolean;
			
			dataXML		= ResourceMgr.instance.getResource(ResourceMgr.XML_HOUSE);
			dataList	= dataXML.house;
			useNewCard	= GameMgr.instance.globalData.newHouseCardEnabled;
			_houseList	= [];
			
			for each(item in xmlList)
			{
				dataXML			= Utils.getXMLByID(item.id, dataList);
				areaList		= item.start_areas.area;
				cardList		= (useNewCard ? dataXML.card_pack_new.card : dataXML.card_pack_old.card);
				vo				= new HouseVO();
				vo.id			= item.id;
				vo.nameEn		= dataXML.name_en;
				vo.nameCh		= dataXML.name_ch;
				vo.color		= dataXML.color;
				vo.hometown		= item.hometown;
				vo.rankThrone	= item.rank_throne;
				vo.rankBlade	= item.rank_blade;
				vo.rankRaven	= item.rank_raven;
				vo.priority		= item.priority;
				vo.cardInHand	= [];
				vo.cardUsed		= [];
				
				addArea(vo, areaList);
				addCard(vo, cardList);
				
				_houseList[vo.id]	= vo;
			}
		}
		
		
		//------------------//
		//     Privates     //
		//------------------//
		
		/**************************************************************************
		 * function				addArea
		 * @desc				add an area to HouseVO
		 * @param	vo			HouseVO
		 * @param	areaList	XML data of the area
		 *************************************************************************/
		private function addArea(vo:HouseVO, areaList:XMLList):void
		{
			var item	:XML;
			var areaID	:String;
			var armyVO	:ArmyVO;
			
			for each(item in areaList)
			{
				areaID			= item.id;
				armyVO			= new ArmyVO();
				armyVO.id		= item.army.id;
				armyVO.amount	= item.army.amount;
				armyVO.house	= vo.id;
				armyVO.area		= areaID;
				
				vo.areas.push(areaID);
				ArmyMgr.instance.setArmyPower(armyVO);
				AreaMgr.instance.setArmy(areaID, armyVO);
			}
		}
		
		/**************************************************************************
		 * function				addCard
		 * @desc				add all cards to HouseVO
		 * @param	vo			HouseVO
		 * @param	cardList	XML data of all cards
		 *************************************************************************/
		private function addCard(vo:HouseVO, cardList:XMLList):void
		{
			var item:XML;
			
			for each(item in cardList)
			{
				vo.cardInHand.push(item.card);
			}
		}
		
		
		private static var _instance:HouseMgr;
		
		private var _houseList	:Object;
	}
	
}